<?php
/**
 * Created by PhpStorm.
 * User: GXF
 * Date: 15-10-14
 * Time: 上午10:31
 */
date_default_timezone_set('PRC');
include 'createTables.php';
session_start();
$action = $_GET['action'];
$db = new SQLite3('MedicalWifi.db');//连接数据库
createDB();
/*****************************************************************************************
 ****************************医生登录******************************************************
 * ***************************************************************************************/
if($action == 'doctorLogin'){
    $id = $_POST['username'];
    $password = $_POST['password'];
    $sql ="select * from doctor where id ='$id' and password ='$password'";
    $result = $db->query($sql);
    if($row = $result -> fetchArray(SQLITE3_ASSOC) ){//存在此医生
        $array = Array('status' => 'success');
        echo json_encode($array);
        $_SESSION["username"] = $id;
    }
    else{//不存在此医生
        $array = Array('status' => 'failed');
        echo json_encode($array);
    }
}
/*****************************************************************************************
****************************医生注册，后需补充~********************************************
 * ***************************************************************************************/
else if($action == 'doctorRegister'){

    //$sql = "insert into doctor values (null,'{$name}','{$password}',${location})";

}
/*****************************************************************************************
 ****************************查询待就诊的病历,自己转出对方已经确认，或者别人转进了，自己确认了**********************************************
 * ***************************************************************************************/
else if($action == 'waitingResume'){
    $doctorID = $_SESSION['username'];
    $array = [];
    $count = 0;
    //我转出，但是对方已经确认
    $sql = "select * from transferRecord where doctorID ='$doctorID' and transferSure =1 and doctorFinishMark=0";
    $result = $db->query($sql);
    if(count($result) > 0){
        while($row = $result->fetchArray()){
            $resumeID = $row['resumeID'];
            $sql1 = "select * from patientResume where id ='{$resumeID}'" ;
            $result1 = $db->query($sql1);
            while($row1 = $result1->fetchArray()){
                $array[$count]['resumeID'] = $resumeID;
                $array[$count]['patientName'] = $row1['patientName'];
                $array[$count]['patientSex'] = $row1['patientSex'];
                $array[$count]['patientBirthday'] = $row1['patientBirthday'];
                $array[$count]['createTime'] = $row1['createTime'];
                $array[$count]['transferDiagnosis'] = $row['transferDiagnosis'];
                $array[$count]['transferMark'] =1;//已经转出
                $count++;
            }
        }
    }
    //转到我这里的
    $sql = "select * from transferRecord where transferDoctorID ='$doctorID' and transferSure =1
            and transferDoctorFinishMark =0";
    $result = $db->query($sql);
    if(count($result) > 0){
        while($row = $result->fetchArray()){
            $resumeID = $row['resumeID'];
            $sql1 = "select * from patientResume where id ='{$resumeID}'" ;
            $result1 = $db->query($sql1);
            while($row1 = $result1->fetchArray()){
                $array[$count]['resumeID'] = $resumeID;
                $array[$count]['patientName'] = $row1['patientName'];
                $array[$count]['patientSex'] = $row1['patientSex'];
                $array[$count]['patientBirthday'] = $row1['patientBirthday'];
                $array[$count]['createTime'] = $row1['createTime'];
                $array[$count]['transferDiagnosis'] = $row['transferDiagnosis'];
                $array[$count]['transferMark'] =0;//表示转入的
                $count++;
            }
        }
    }
    if($count > 0){
        echo json_encode(array('count'=>$count,'data'=>$array));
    }
    else{
        echo json_encode(array('count'=>0,'data'=>''));
    }
}
/******************************************************************************************
 ****************************查询待确认的病历，包括未有医生接收，以及转诊过来的***************
 * ****************************************************************************************/
else if($action == 'waitingSure'){
    $doctorID = $_SESSION['username'];
    $array = [];
    $count = 0;
    //转给我的,未确认
    $sql = "select * from transferRecord where transferDoctorID ='$doctorID' and transferSure =0";
    $result = $db->query($sql);
    if(count($result) > 0){
        while($row = $result->fetchArray()){
            $resumeID = $row['resumeID'];
            $sql1 = "select * from patientResume where id ='{$resumeID}'" ;
            $result1 = $db->query($sql1);
            while($row1 = $result1->fetchArray()){
                $array[$count]['resumeID'] = $resumeID;
                $array[$count]['patientName'] = $row1['patientName'];
                $array[$count]['patientSex'] = $row1['patientSex'];
                $array[$count]['patientBirthday'] = $row1['patientBirthday'];
                $array[$count]['createTime'] = $row1['createTime'];
                $array[$count]['transferDiagnosis'] = $row['transferDiagnosis'];
                $array[$count]['transferMark'] =0;//表示转入的
                $count++;
            }
        }
    }
    //有我转出，未确认
    $sql = "select * from transferRecord where doctorID ='$doctorID' and transferSure =0";
    $result = $db->query($sql);
    if(count($result) > 0){
        while($row = $result->fetchArray()){
            $resumeID = $row['resumeID'];
            $sql1 = "select * from patientResume where id ='{$resumeID}'" ;
            $result1 = $db->query($sql1);
            while($row1 = $result1->fetchArray()){
                $array[$count]['resumeID'] = $resumeID;
                $array[$count]['patientName'] = $row1['patientName'];
                $array[$count]['patientSex'] = $row1['patientSex'];
                $array[$count]['patientBirthday'] = $row1['patientBirthday'];
                $array[$count]['createTime'] = $row1['createTime'];
                $array[$count]['transferDiagnosis'] = $row['transferDiagnosis'];
                $array[$count]['transferMark'] =1;//表示转出的
                $count++;
            }
        }
    }
    echo json_encode(array('count'=>$count,'data'=>$array));
}
/*****************************************************************************************
 ***************************对某一病历进行转诊*********************************************
 * ***************************************************************************************/
else if($action == 'transfer'){
    $doctorID = $_SESSION['username'];
    $resumeID = $_POST['resumeID'];
    $transferDoctorID = $_POST['transferDoctorID'];
    $transferDiagnosis = $_POST['transferDiagnosis'];
    $date = date("Y-m-d H:i:s");
    $transferID = '';
    $flag = 1;
    while($flag){
        $a = mt_rand(10000,99999);
        $b = mt_rand(10000,99999);
        $transferID = $a.$b;//随机生成20位不重复id
        $sql = "select * from transferRecord where transferID= '$transferID'";
        $result = $db->query($sql);
        if(!($row = $result -> fetchArray(SQLITE3_ASSOC)) ){//若数据库中不存在此id
            $flag = 0;
        }
    }
    $sql = "UPDATE patientResume SET waitingDoctorMark=0,finish=1 WHERE id='$resumeID'";
    $result1 = $db->exec($sql);
    $sql = "INSERT INTO transferRecord VALUES ('{$transferID}','{$resumeID}','{$doctorID}',0,0,0,'{$transferDoctorID}','{$transferDiagnosis}',0,'{$date}')";
    $result = $db->exec($sql);
    if($result && $result1){
        echo json_encode(array('status'=>'success'));
    }
    else json_encode(array('status'=>'failed'));
}
/*****************************************************************************************
 ****************************查询可将此病例转给的医生**************************************
 * ***************************************************************************************/
else if($action == 'availableDoctor'){
    $doctorID = $_SESSION['username'];
    $location = $_GET['location'];
    $sql ="select * from doctor where location ='$location' and id !='$doctorID'";
    $result = $db->query($sql);
    $array = [];
    $count = 0;
    if($result){
        while($row = $result->fetchArray()){
            $array[$count] = $row;
            $count++;
        }
        echo json_encode(array('count'=>$count,'data'=>$array));
    }
    else{
        echo json_encode(array('count'=>0,'data'=>''));
    }
}
/*****************************************************************************************
 ****************************查询患者信息,入：未转诊的****************************************************
 *                                       出： 我转出去的**************************************************
 * ***************************************************************************************/
else if($action == 'patientSearch'){
    $doctorID = $_SESSION['username'];
    //未转诊
    $sql ="select * from patientResume where waitingDoctorMark =1";
    $result = $db->query($sql);
    $array = [];
    $count = 0;
    while($row = $result->fetchArray()){
        //$array[$count] = $row;
        //此处，若transferMark=0，未转诊，transferMark=1，显示出，已经转诊，
        $array[$count]['transferMark'] =2;
        $array[$count]['id'] = $row['id'];
        $array[$count]['patientName'] = $row['patientName'];
        $array[$count]['patientSex'] = $row['patientSex'];
        $array[$count]['patientBirthday'] = $row['patientBirthday'];
        $array[$count]['createTime'] = $row['createTime'];
        $count++;
    }
    //已转出
    $sql ="select * from transferRecord where doctorID ='$doctorID' and doctorFinishMark =0";
    $result = $db->query($sql);
    while($result && $row = $result->fetchArray()){
        $resumeID = $row['resumeID'];
        $sql1 = "select * from patientResume where id ='{$resumeID}'" ;
        $result1 = $db->query($sql1);

        while($row1 = $result1->fetchArray()){
            $array[$count] = $row1;
            //此处，若transferAgainMark=0，则显示入，并未转诊，transferAgainMark=1，显示出，已经转诊，
            $array[$count]['transferMark'] =1;
            $array[$count]['patientName'] = $row1['patientName'];
            $array[$count]['patientSex'] = $row1['patientSex'];
            $array[$count]['patientBirthday'] = $row1['patientBirthday'];
            $array[$count]['createTime'] = $row1['createTime'];
            $array[$count]['transferID'] =$row['transferID'];
            $transferDoctorID = $row['transferDoctorID'];
            $sql2 = "select * from doctor where id ='{$transferDoctorID}'" ;
            $result2 = $db->query($sql2);
            while($row2 = $result2 -> fetchArray()){
                $array[$count]['hospital'] =$row2['hospital'];
                $array[$count]['location'] =$row2['location'];
                $array[$count]['doctorName'] =$row2['name'];
            }
            $count++;
        }
    }
    if($count>0){
        echo json_encode(array('count'=>$count,'data'=>$array));
    }
    else{
        echo json_encode(array('count'=>0,'data'=>''));
    }
}
/*****************************************************************************************
 ****************************完成在我处的流程,两种情况 我转出去的finish，转给我的finish**************************************************
 * ***************************************************************************************/
else if($action == 'finish'){
    $id = $_SESSION['username'];
    $resumeID = $_GET['resumeID'];
    $kind = $_GET['kind'];
    if($kind == 1)      //我转出的
        $sql = "update transferRecord set doctorFinishMark =1 where resumeID='$resumeID' and doctorID='$id'";
    else if($kind == 0)//转给我的
        $sql = "update transferRecord set transferDoctorFinishMark =1 where resumeID='$resumeID' and transferDoctorID='$id'";
    else{
        $sql = "SELECT * FROM transferRecord where resumeID='$resumeID' and doctorID='$id'";
        $result = $db->query($sql);
        if($result->fetchArray(SQLITE3_ASSOC)){
            $sql = "update transferRecord set doctorFinishMark =1 where resumeID='$resumeID' and doctorID='$id'";
        }
        else{
            $sql = "update transferRecord set transferDoctorFinishMark =1 where resumeID='$resumeID' and transferDoctorID='$id'";
        }
    }
    $result = $db->exec($sql);
    if($result)
        echo json_encode(array('status'=>'success'));
    else
        echo json_encode(array('status'=>'failed'));
}
/*****************************************************************************************
 ****************************在待确定页面，点击了确认按钮，*********************************
 * **************************就是别人转给我的***********************************************
 * ***************************************************************************************/
else if($action == 'true'){
    $id = $_SESSION['username'];
    $resumeID = $_GET['resumeID'];
    $sql = "update transferRecord set transferSure =1 where resumeID='$resumeID' and transferDoctorID='$id'";
    $result = $db->exec($sql);
    if($result)
        echo json_encode(array('status'=>'success'));
    else
        echo json_encode(array('status'=>'failed'));
}
/*****************************************************************************************
 ****************************在待确定页面，点击重新发起按钮*********************************
 * **************************删除原来的转诊，重新转诊***********************************************
 * ***************************************************************************************/
else if($action == 'reTransfer'){
    $doctorID = $_SESSION['username'];
    $resumeID = $_POST['resumeID'];
    $transferDoctorID = $_POST['transferDoctorID'];
    $transferDiagnosis = $_POST['transferDiagnosis'];
    $sql = "DELETE FROM transferRecord WHERE resumeID='$resumeID' AND doctorID='$doctorID'";
    $result1 = $db->exec($sql);
    $date = date("Y-m-d H:i:s");
    $transferID = '';
    $flag = 1;
    while($flag){
        $a = mt_rand(10000,99999);
        $b = mt_rand(10000,99999);
        $transferID = $a.$b;//随机生成20位不重复id
        $sql = "select * from transferRecord where transferID= '$transferID'";
        $result = $db->query($sql);
        if(!($row = $result -> fetchArray(SQLITE3_ASSOC)) ){//若数据库中不存在此id
            $flag = 0;
        }
    }
    $sql = "INSERT INTO transferRecord VALUES ('{$transferID}','{$resumeID}','{$doctorID}',0,0,0,'{$transferDoctorID}','{$transferDiagnosis}',0,'{$date}')";
    $result = $db->exec($sql);
    if($result && $result1){
        echo json_encode(array('status'=>'success'));
    }
    else json_encode(array('status'=>'failed'));
}
else if($action = 'transferCancel'){
    $doctorID = $_SESSION['username'];
    $resumeID = $_GET['resumeID'];
    $sql = "DELETE FROM transferRecord WHERE resumeID ='$resumeID'";
    $result = $db->exec($sql);
    $sql1 = "UPDATE patientResume SET waitingDoctorMark = 1,finish = 0 WHERE id='$resumeID'";
    $result1 = $db->exec($sql1);
    if($result1 && $result)
        echo json_encode(array('status'=>'success'));
    else
        echo json_encode(array('status'=>'failed'));
}